(function(angular) {
	'report strict';
	angular.module('fs.waf').controller('cda.report.createRejectController', createRejectController);
	createRejectController.$inject = [ '$scope', 'Constants', 'modalService', 'gridService', 'layoutConfig', 'validatorConfig', 'authService','storageService',
			'reportService' ];
	function createRejectController($scope, Constants, modalService, gridService, layoutConfig, validatorConfig, authService, storageService, reportService) {
		var ctrl = this, params = $scope.$stateParams.params, errorMessage = {};
		ctrl.validatorConfig = validatorConfig;
		ctrl.report = {};
		
		ctrl.init = function() {
			// init report Parameter
			reportService.initDataOptions($scope, ctrl);
			authService.createReject(params).then(function(response) {
				if (response.data && response.data.model && response.data.task) {
					// init report Parameter
					reportService.initReportParameter($scope, ctrl, layoutConfig);
					// init report company
					reportService.initCompany($scope, ctrl, false, layoutConfig);
					ctrl.report = response.data.model;
					ctrl.report.taskId = response.data.task.taskId;
					ctrl.report.reportParms = ctrl.report.reportParms || [];
					ctrl.report.companyRpts = ctrl.report.companyRpts || [];
					ctrl.judge = "Y";
					ctrl.layoutConfig = layoutConfig;
				}
			});
		};
		
		ctrl.companyCodeChanged = function(row, form, isFirst) {
			if(isFirst){
				return;
			}
			if ( !ctrl.report.companyCode ) {
				ctrl.report.companyCode = '*';
				ctrl.report.companyName = '*';
			}
			
			ctrl.report.companyRpts = [];
			if(ctrl.report.companyCode != "ALL" && ctrl.report.companyCode != "*"){
				ctrl.companyGridOptions.enableOperation = false;
				ctrl.companyGridOptions.enableOperationAdd = false;
				ctrl.companyGridOptions.enableRowHeaderSelection = false;
				ctrl.companyGridOptions.enableOperationRemoveSelected=false;
			}else{
				ctrl.companyGridOptions.enableOperation = true;
				ctrl.companyGridOptions.enableOperationAdd = true;
				ctrl.companyGridOptions.enableRowHeaderSelection = true;
				ctrl.companyGridOptions.enableOperationRemoveSelected = true;
			}
			
			if(ctrl.judge == "Y"){
				ctrl.judge = "N";
			}else if(ctrl.judge == "N"){
				modalService.openConfirmDialogs({
					'messages' :  [ 'title.dialog.confirm.continue']
				});
			}
		};
		
		ctrl.changeCompanyTypes = function(row, form, isFirst) {
			if(isFirst){
				return;
			}
			ctrl.report.companyRpts = [];
		};
		
		ctrl.changeMode = function(){
			if ( ctrl.report && ctrl.report.reportMode){				
				if(ctrl.report.reportMode == "O"){
					ctrl.report.archiveMethod = "N";
					ctrl.report.reportUrl = "cda.reportView.defaultView";
				}else if(ctrl.report.reportMode == "B"){
					ctrl.report.archiveMethod = "D";
					ctrl.report.reportUrl = "com.pti.fsc.cda.ui.report.ReportRunnerDefault";
				}else if(ctrl.report.reportMode == "I"){
					ctrl.report.archiveMethod = "N";
					ctrl.report.reportUrl = "cda.reportView.defaultView";
				}
				
			} else {
				if ( ctrl.report){
					ctrl.report.reportUrl = "cda.reportView.defaultView";
				}
			}
		};
		
		ctrl.createRejectSave = function() {
			ctrl.report.reportTemplateDocList = ctrl.report.reportTemplateDocList || [];
			if(ctrl.report.reportTemplateDocList.length > 0){
				ctrl.report.docId = ctrl.report.reportTemplateDocList[0].docId;
			}
			if(angular.equals(ctrl.report.isAdhocReport,"N")){
				delete ctrl.report.reportAd;
			}else{
				ctrl.report.reportAd = ctrl.report.reportAd || {};
			}
			return authService.createRejectSave(ctrl.report).then(saveSuccessful, saveFailed);
		};

		ctrl.createRejectSubmit = function() {
			ctrl.report.reportTemplateDocList = ctrl.report.reportTemplateDocList || [];
			if(ctrl.report.reportTemplateDocList.length > 0){
				ctrl.report.docId = ctrl.report.reportTemplateDocList[0].docId;
			}
			if(angular.equals(ctrl.report.isAdhocReport,"N")){
				delete ctrl.report.reportAd;
			}else{
				ctrl.report.reportAd = ctrl.report.reportAd || {};
			}
			return authService.createRejectSubmit(ctrl.report).then(submitSuccessful, submitFailed);
		};

		ctrl.createRejectCancel = function() {
			modalService.openConfirmDialogs({
				'messages' : [ {msg : 'title.dialog.confirm.cancel', args : [ctrl.report.primaryKey]} ],
				'btnCallback' : {
					'ok' : function() {
						authService.createRejectCancel(ctrl.report).then(cancelSuccessful, cancelFailed);
					}
				}
			});
		};
		
		function submitSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.submit' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.report.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.report.list',
					'state' : 'cda.report.list',
					'params' : {}
				}]
			});
		}

		function submitFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}

		function saveSuccessful(response) {
			modalService.openInfoDialogs({
				'messages' : [ 'title.dialog.successful.save' ]
			});
			if (response.data && response.data.model) {
				angular.extend(ctrl.report, response.data.model);
			}else if (response.data.task && response.data.task.taskId) {
				authService.saveSuccessfulProcess(response.data.task).then(function(response) {
					var routerInfo = response.data || {}, state = routerInfo.processState;
					delete routerInfo.processState;
					$scope.go(state, routerInfo);
				}, angular.noop);
			}
		}

		function saveFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
		
		function cancelSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.cancel' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.report.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.report.list',
					'state' : 'cda.report.list',
					'params' : {}
				}]
			});
		}

		function cancelFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
		
		ctrl.addCompany = function() {
			var params = {};
			params.companyCodes = [];
			params.reportFormats = ctrl.report.reportFormats;
			params.reportFormatsOptions = reportService.getReportFormatsOptions();
			angular.forEach(ctrl.report.companyRpts, function(company, index) {
				params.companyCodes.push(company.companyCode);
			});
			modalService.openCustomDialogs({
				'dialogsModule' : 'cda.reportCompany',
				'params' : params,
				'btnCallback' : {
					'ok' : function(obj) {
						var selectedRows = obj.selectedRows;
						if (!selectedRows || selectedRows.length == 0) {
							modalService.openErrorDialogs({
								messages : [ 'title.dialogs.info.norecsel' ]
							});
							return false;
						}
						if (!ctrl.report.companyRpts) {
							ctrl.report.companyRpts = [];
						}
						selectedRows[0].dataStatus = 'C';
						var format = ",";
						angular.forEach(obj.reportCompany.reportFormats, function(reportFormat, index) {
							format = format + reportFormat+",";
						});
						selectedRows[0].reportFormats = format;
						ctrl.report.companyRpts.push(selectedRows[0]);
						event.preventDefault();
						event.stopPropagation();
					}
				}
			});
		};
		
		ctrl.viewRole = function() {
			modalService.openCustomDialogs({
				'dialogsModule' : 'cda.right',
				'btnCallback' : {
					'ok' : function(selectedRows) {
						
					}
				}
			});
		};
		
		
		function rowSelectionCompanyChanged(eventRow, selectedRows) {
			ctrl.selectedCompanyRpts = selectedRows;
		}
		
		function rowSelectionChanged(eventRow, selectedRows) {
			ctrl.selectedParameters = selectedRows;
		}
		
		ctrl.addReportParameter = function() {
			ctrl.report.reportParms.push({
			});
		};

	}
})(angular);